import { defineStore } from 'pinia'
import { getHouseList, getHouseDetail, searchHouse, addHouse, updateHouse, deleteHouse, getHotHouses, getLatestHouses } from '../api/house'

export const useHouseStore = defineStore('house', {
    state: () => ({
        houseList: [],
        hotHouses: [],
        latestHouses: [],
        currentHouse: {},
        total: 0,
        loading: false,
        // 状态值定义:
        // 0 - 待审核/审核中
        // 1 - 在售/上架
        // 2 - 已售/已售出
        // 3 - 下架/已下架
        // 4 - 审核未通过
    }),

    actions: {
        // 获取房源列表
        async getHouseList(params = {}) {
            try {
                this.loading = true
                const res = await getHouseList(params)
                if (res.code === 200) {
                    this.houseList = res.data.records
                    this.total = res.data.total
                    return res.data
                }
            } catch (error) {
                console.error('获取房源列表失败：', error)
                throw error
            } finally {
                this.loading = false
            }
        },

        // 获取房源详情
        async getHouseDetail(id) {
            try {
                const res = await getHouseDetail(id)
                if (res.code === 200) {
                    this.currentHouse = res.data
                    return res.data
                }
            } catch (error) {
                console.error('获取房源详情失败：', error)
                throw error
            }
        },

        // 搜索房源
        async searchHouse(params) {
            try {
                this.loading = true
                const res = await searchHouse(params)
                if (res.code === 200) {
                    this.houseList = res.data.records
                    this.total = res.data.total
                    return res.data
                }
            } catch (error) {
                console.error('搜索房源失败：', error)
                throw error
            } finally {
                this.loading = false
            }
        },

        // 新增房源
        async addHouse(data) {
            try {
                const res = await addHouse(data)
                if (res.code === 200) {
                    return res.data
                }
            } catch (error) {
                console.error('新增房源失败：', error)
                throw error
            }
        },

        // 修改房源
        async updateHouse(data) {
            try {
                const res = await updateHouse(data)
                if (res.code === 200) {
                    return res.data
                }
            } catch (error) {
                console.error('修改房源失败：', error)
                throw error
            }
        },

        // 删除房源
        async deleteHouse(id) {
            try {
                const res = await deleteHouse(id)
                if (res.code === 200) {
                    return res.data
                }
            } catch (error) {
                console.error('删除房源失败：', error)
                throw error
            }
        },

        // 获取热门房源列表
        async getHotHouses(params = {}) {
            try {
                this.loading = true
                const res = await getHotHouses(params)
                if (res.code === 200) {
                    this.hotHouses = res.data.records
                    this.total = res.data.total
                    return res.data
                }
            } catch (error) {
                console.error('获取热门房源列表失败：', error)
                throw error
            } finally {
                this.loading = false
            }
        },

        // 获取最新房源列表
        async getLatestHouses(params = {}) {
            try {
                this.loading = true
                const res = await getLatestHouses(params)
                if (res.code === 200) {
                    this.latestHouses = res.data.records
                    this.total = res.data.total
                    return res.data
                }
            } catch (error) {
                console.error('获取最新房源列表失败：', error)
                throw error
            } finally {
                this.loading = false
            }
        }
    }
}) 